/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;
import org.mags.object.common.ParserFormatterUtils;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.view.commons.RelationshipViewBase;

/**
 * 
 * @author Mario Gaitan
 *
 */
class Property {
	private String property;
	private String value;
	public Property() {
	}
	public String getProperty() {
		return property;
	}
	public void setProperty(String property) {
		this.property = property;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

public class PropertiesView extends RelationshipViewBase 
	implements IStructuredContentProvider, ITableLabelProvider  {
	public final static String ID="org.mags.remas.view.propertiesView";
	private TableViewer tableViewer;
	private EntityRelationship entityRelationship;
	public EntityRelationship getEntityRelationship() {
		return entityRelationship;
	}
	public void setEntityRelationship(EntityRelationship entityRelationship) {
		this.entityRelationship = entityRelationship;
	}
	public void refresh() {
		tableViewer.refresh();
	}
	@Override
	public void createPartControl(Composite parent) {
		tableViewer = new TableViewer(parent, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL|SWT.FULL_SELECTION);
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.getTable().setLinesVisible(true);
		
		tableViewer.setLabelProvider(this);
		tableViewer.setContentProvider(this);
		tableViewer.setInput(this);
		
		TableColumn column;
		column = new TableColumn(tableViewer.getTable(), SWT.LEFT);
		column.setText("property");
		column.setWidth(150);
		column.setResizable(true);
		column.setToolTipText("property");
		column.setMoveable(false);
		
		column = new TableColumn(tableViewer.getTable(), SWT.LEFT|SWT.MULTI);
		column.setText("value");
		column.setWidth(150);
		column.setResizable(true);
		column.setToolTipText("value");
		column.setMoveable(false);			
	}
	@Override
	public void setFocus() {
		tableViewer.getTable().setFocus();
	}
	@Override
	public Object[] getElements(Object inputElement) {
		if(entityRelationship!=null) {
			Property property;
			List<Property> properties = new ArrayList<Property>();
			property = new Property();
			properties.add(property);
			property.setProperty("name");
			property.setValue(entityRelationship.getEntity().getName());
			property = new Property();
			properties.add(property);
			property.setProperty("code");
			property.setValue(entityRelationship.getEntity().getCode());
			property = new Property();
			properties.add(property);
			property.setProperty("created by");
			property.setValue(entityRelationship.getEntity().getCreatedBy());
			property = new Property();
			properties.add(property);
			property.setProperty("creation date");
			property.setValue(ParserFormatterUtils.getInstance().date2Text(entityRelationship.getEntity().getCreationDate()));
			property = new Property();
			properties.add(property);
			property.setProperty("updated by");
			property.setValue(entityRelationship.getEntity().getUpdatedBy());
			property = new Property();
			properties.add(property);
			property.setProperty("change date");
			property.setValue(ParserFormatterUtils.getInstance().date2Text(entityRelationship.getEntity().getChangeDate()));
			property = new Property();
			properties.add(property);
			property.setProperty("type");
			if(entityRelationship.getEntity().getEntityType()!=null)
				property.setValue(entityRelationship.getEntity().getEntityType().getName());
			else
				property.setValue("");
			if(entityRelationship.getLink()!=null) {
				property = new Property();
				properties.add(property);
				property.setProperty("link to");
				property.setValue(entityRelationship.getLink().getEntity().getName());
			}

			
			return properties.toArray();
		} else
			return new Object[0];
	}
	@Override
	public String getColumnText(Object element, int columnIndex) {
		Property property = (Property)element;
		if(columnIndex==0)
			return property.getProperty();
		else
			return property.getValue();
	}	
	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		return null;
	}
	@Override
	public void addListener(ILabelProviderListener listener) {
	}
	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}
	@Override
	public void removeListener(ILabelProviderListener listener) {
	}
}
